package com.ztesoft.web.resourcecheck;

import java.text.MessageFormat;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.ztesoft.core.db.ds.DynamicDataSourceManager;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.exception.ExceptionHandler;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.db.po.DmIndexInfoPO;


/**
 * 表索引查询<br>
 * 
 * @author <a href="mailto:lin.weibin@zte.com.cn">lin.wb</a> 2016年5月4日 上午10:50:21
 * @version 1.0
 */
@Service
public class IdxCheck {

	private static final ZTEsoftLogManager LOGGER = ZTEsoftLogManager.getLogger(IdxCheck.class);
	
	@Autowired
	private DynamicDataSourceManager dynamicDataSourceManager;
	
	/**
	 * 表索引查询
	 * @param dbLinkId 数据库实例表标志
	 * @param tableName 查询的表名
	 * @param dbConfig 数据库配置信息 （{数据库类型<小写>0，数据库连接1，用户名2，密码3,数据库名4}) 通过baseinfoservice.getDbConfig()获取 
	 * @throws BaseAppException 
	 */
	public List<Map<String, Object>> qryIdx(Integer dbLinkId, String tableName, String[] dbConfig) throws BaseAppException {
		LOGGER.debug("表索引查询  ..");
		String qrySql = DMSConstant.IDX_SQL_MAP.get(dbConfig[0]);//获取得到sql
		qrySql = MessageFormat.format(qrySql, StringUtils.join("'", dbConfig[4], "'"));//慎用，会把'去掉
		try {			
			DataSource dataSource = 
					dynamicDataSourceManager.getDruidDataSource(dbLinkId+dbConfig[2], dbConfig);
			return new JdbcTemplate(dataSource).queryForList(qrySql, tableName);
		} catch (Exception e) {
			LOGGER.error("{0}表索引查询异常{1}",dbConfig[0],e.getMessage());
			throw ExceptionHandler.publish("表索引查询异常", e);
		}
	}
	
	/**
	 * 根据约定转化bean对象集合
	 * @param dbLinkId
	 * @param tableName
	 * @param dbConfig
	 * @return
	 * @throws BaseAppException
	 */
	public List<DmIndexInfoPO> qryIdxForPO(Integer dbLinkId, String tableName, String[] dbConfig) throws BaseAppException {
		try {
			LOGGER.debug("表索引查询  ..");
			String qrySql = DMSConstant.IDX_SQL_MAP.get(dbConfig[0]);//获取得到sql
			qrySql = MessageFormat.format(qrySql, StringUtils.join("'", dbConfig[4], "'"));//慎用，会把'去掉
			LOGGER.debug("表索引查询  ..{0}",qrySql);
			tableName = DMSConstant.ORACLE.equals(dbConfig[0]) ? StringUtils.upperCase(tableName) : StringUtils.lowerCase(tableName); 
			DataSource dataSource = 
					dynamicDataSourceManager.getDruidDataSource(dbLinkId+dbConfig[2], dbConfig);
			qrySql = qrySql.replace("?", StringUtils.join("'", tableName, "'"));
			LOGGER.debug("表索引查询  ..{0}",qrySql);
			return new JdbcTemplate(dataSource).query(qrySql, new BeanPropertyRowMapper<DmIndexInfoPO>(DmIndexInfoPO.class));

		} catch (Exception e) {
			LOGGER.error("{0}表索引查询异常{1}",dbConfig[0],e.getMessage());
			throw ExceptionHandler.publish("表索引查询异常", e);
		}
	}
	
}
